// hyb_yl/twosubpages/pages/publicProblemsInfor/publicProblemsInfor.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: !0,
    isfollow: '关注',
    starIcon: 3,
    tw: '问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题',
    hd: '回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答回答',
    shareState: false,
    maskey: true,
    maskbtn_key: true,
  },
  // 取消遮罩层
  mask_key: function () {
    this.setData({
      maskey: true
    })
  },
  followtab: function (e) {
    var openid = wx.getStorageSync('openid');
    if(openid == '')
    {
      wx.showModal({
        title:"提示",
        content:"请先登录",
        success:function(con)
        {
          if(con.confirm)
          {
            wx.navigateTo({
              url: '/hyb_yl/sgin/sgin',
            })
          }else{
            wx.switchTab({
              url: '/hyb_yl/tabBar/index/index',
            })
          }
        }
      })
    }else{
      var zid = e.currentTarget.dataset.zid;
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.changelove',
      data: {
        zid: zid,
        openid: wx.getStorageSync('openid'),
        cerated_type: 7
      },
      success: function (res) {
        console.log(res)
      }
    })
    if (this.data.isfollow == '关注') {
      this.setData({
        isfollow: '已关注'
      })
    } else {
      this.setData({
        isfollow: '关注'
      })
    }
    }
    
  },
  tiwen: function (e) {
    var openid = wx.getStorageSync('openid');
    if(openid == '')
    {
      wx.showModal({
        title: '提示',
        content:"请先登录",
        success:function(con)
        {
          if(con.confirm)
          {
            wx.navigateTo({
              url: '/hyb_yl/sgin/sgin',
            })
          }else{
            wx.switchTab({
              url: '/hyb_yl/tabBar/index/index',
            })
          }
        }
      })
    }else{
      var zid = e.currentTarget.dataset.zid;
      if (zid != '' && zid != 'null' && zid != null) {
        wx.navigateTo({
          url: '/hyb_yl/czhuanjiasubpages/pages/zhuanjiazhuye/zhuanjiazhuye?zid=' + zid + '&tit=图文问诊&ser_key=tuwenwenzhen&long=&id=&hid=&conets=&j_id=&typs=',
        })
      }
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    var typs = options.typs
    var state = options.state
    that.setData({
      id: id,
      typs: typs,
      state: state
    })
    that.getAnswer();
  },
  getAnswer: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/answer.answer_detail',
      data: {
        id: that.data.id,
        typs: that.data.typs,
        state: that.data.state,
        openid: wx.getStorageSync('openid')
      },
      success: function (res) {
        console.log(res)
        that.setData({
          info: res.data,
          isLoading: !1
        })
        app.util.request({
          'url': 'entry/wxapp/zhuanjia.ifguanzhu',
          data: {
            zid: res.data[0].zid,
            openid: wx.getStorageSync('openid'),
            cerated_type: 7
          },
          success(res) {
            console.log(res)
            if (res.data == 1) {
              that.setData({
                isfollow: '已关注'
              })
            } else {
              that.setData({
                isfollow: '关注'
              })
            }
          }
        })
        wx.setNavigationBarTitle({
          title: res.data.title,
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  shareChose: function (e) {
    this.setData({
      shareState: true
    })
  },
  closeShare: function (e) {
    this.setData({
      shareState: false
    })
  },
  sharefri: function (e) {
    var that = this
    var id = that.data.id
    var typs = that.data.typs
    var state = that.data.state
    app.util.request({
      'url': 'entry/wxapp/answer.generate2',
      data: {
        id: id,
        typs: typs,
        state: state
      },
      success: function (res) {
        console.log(res)
        that.setData({
          img: res.data,

        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
    this.setData({
      maskey: false,
      shareState: false,
    })
  },
  
  // 下载图片
  maskbtn: function () {
    // 保存到相册
    let that = this
    const imageFile = that.data.img
    wx.showLoading({
      title: '图片保存中...'
    })
    wx.downloadFile({
      url: imageFile,
      success: function (res) {
        // 图片保存到本地
        console.log(res.tempFilePath)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.hideLoading()
            wx.showModal({
              title: '图片已存入相册',
              content: '好东西要分享，发给朋友们看看。',
              showCancel: false
            })
          },
          fail: function (err) {
            console.log(err)
            if (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg === 'saveImageToPhotosAlbum:fail auth deny' || err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
              console.log('22222222222222222222222222222222=============================')
              // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
              wx.showModal({
                title: '提示',
                content: '需要您授权保存相册',
                showCancel: false,
                success: modalSuccess => {
                  wx.openSetting({
                    success(settingdata) {
                      console.log('settingdata', settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限成功,再次点击按钮即可保存',
                          showCancel: false
                        })
                      } else {
                        wx.showModal({
                          title: '提示',
                          content: '获取权限失败，将无法保存到相册！',
                          showCancel: false
                        })
                      }
                    },
                    fail(failData) {
                      console.log('failData', failData)
                    },
                    complete(finishData) {
                      console.log('finishData', finishData)
                    }
                  })
                }
              })
            }
          },
          complete(res) {
            wx.hideLoading()
          }
        })
      }
    })
  },
})